package question.solution91;

import question.SolutionTemplate;

import java.util.List;

public class Solution91 implements SolutionTemplate {

    public int numDecodings(String s) {
        char[] sArr = s.toCharArray();
        int[] codeCountArr = new int[s.length()];
        if(judgeQualifiedNumber(sArr, -1, 0)){
            codeCountArr[0]=1;
        }
        if(s.length() == 0){
            return 0;
        }
        if(s.length() == 1){
            return codeCountArr[0];
        }
        if(judgeQualifiedNumber(sArr, -1, 1)){
            codeCountArr[1]=codeCountArr[0];
        }
        if(judgeQualifiedNumber(sArr, 0, 1)){
            codeCountArr[1]++;
        }
        for(int i =2;i<s.length();i++){
            if(judgeQualifiedNumber(sArr, i-1,i)){
                codeCountArr[i] = codeCountArr[i-2];
            }
            if(judgeQualifiedNumber(sArr, -1,i)){
                codeCountArr[i] += codeCountArr[i-1];
            }
        }
        return codeCountArr[s.length()-1];
    }

    private boolean judgeQualifiedNumber(char[] arr, int firstIndex, int secondIndex){
        short firstVal;
        if(firstIndex == -1){
            firstVal = 0;
        }else{
            if(arr[firstIndex] == '0'){
                return false;
            }
            firstVal = (short)((arr[firstIndex]-48)*10);
        }
        short val = (short)(firstVal + (arr[secondIndex]-48));
        if(val <=26 && val >=1){
            return true;
        }
        return false;
    }


    public void test() {
//        char a ='9';
//        System.out.println(a-48);
        String str = "06";
        int result = numDecodings(str);
        return;
    }
}
